//获取应用实例
import Promise from '../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../utils/regenerator-runtime/runtime'
const utils = require('../../utils/util.js');
const app = getApp()
Page({
  data: {
    userInfo: null,
    text:"点击进入",
    getFormIdType:true,
    getUserInfoType:true,
    token:null,
    isLoaded:false,
    isHide:false
  },
  onShow:function(){
   // if(this.data.isHide){
      this.setData({
        isHide:false,
        userInfo: null,
        text:"点击进入",
        getFormIdType:true,
        getUserInfoType:true,
        token:null,
        isLoaded:false
      })
      this.onLoad();
   // }
  },
  onHide:function(){
    this.setData({
      isHide:true
    })
  },
  onLoad: async function(params) {
    wx.showLoading({
      title:"加载中..."
    })
    console.log("res_code",params.res_code);
    var token = await app.getToken(true,params.res_code);
    if(token==null){
      wx.hideLoading();
      wx.showModal({
        title: '错误提示',
        content: '登录失败，请重试',
        showCancel:false,
        success:res=>{
          this.onLoad();
        }
      })
      return;
    }
    var data=await app.getLeftFormId(token); 
    if(data.state){
      var data=data.data;
      this.setData({
        getFormIdType:data.leftcount<=20,
        usertype:data.usertype,
      })
      app.globalData.usertype=data.usertype;
      if(data.nickname!="" && data.headimg!=""){
        this.setData({
          userInfo:{
            nickname:data.nickname,
            headimg:data.headimg
          },
          getUserInfoType:false
        })
        app.globalData.userInfo=this.data.userInfo;
        if(!this.data.getFormIdType) this._jumpUrl();
      }
    }
    
    console.log("onLoad-page");
    var that=this;
    var authSetting=await this._getSetting();
    // 查看是否授权
    if (authSetting.authSetting['scope.userInfo']) {
      that.setData({
        getUserInfoType:false
      })
    }
    that.setData({
      isLoaded:true
    })
    wx.hideLoading();
  },
  _getSetting:function(){
    var p = new Promise((resolve,reject)=>{
      //做一些异步操作
      wx.getSetting({
        success: function(res){
          resolve(res);
        },
        error:function(res){
          resolve(res);
        }
      })
  });
  return p;
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail);
    let formid = e.detail.formId;
    this.detailFromIds(formid);
  },
  detailFromIds: function (formId){
    let formIds=app.globalData.globalDataFormIds || [];
    let data={"formId":formId,"expire":parseInt(new Date().getTime()/1000)+604800}
    formIds.push(data);
    app.globalData.globalDataFormIds = formIds;
    this.setData({
      globalDataFormIds: JSON.stringify(formIds)
    })
  },
  bindGetUserInfo:function(e){
    if(!this.data.isLoaded) return;
    console.log(e.detail.userInfo)
    if (e.detail.userInfo){
      //用户按了允许授权按钮
      console.log("允许");
      var text="授权成功，请稍后...";
      this.setData({
        userInfo:{
          nickname:e.detail.userInfo.nickName,
          headimg:e.detail.userInfo.avatarUrl
        },
        getFormIdType:false,
        text:text
      })
      app.globalData.userInfo=this.data.userInfo;
      this._setUserInfo();
    } else {
      //用户按了拒绝按钮
      console.log("拒绝");
      var text="授权失败，请重试";
      this.setData({
        text:text
      })
    }
  },
  _jumpUrl:function(){
    if(this.data.usertype==1){
      wx.switchTab({
        url:"../my/info/info"
      })
    }
    else{
      wx.redirectTo({
        url:"../sales/info/info"
      })
    }
  },
  _setUserInfo:function(){
    var token=wx.getStorageSync('token');
    console.log(token);
    app.request("/api/message/doFormid",{formid:JSON.stringify(app.globalData.globalDataFormIds),token:token},res=>{
      console.log(res);
      app.globalData.globalDataFormIds=null;
      app.request("/api/message/putUserInfo",{nickname:this.data.userInfo.nickname,headimg:this.data.userInfo.headimg,token:token},res=>{
        console.log(res);
        // wx.switchTab({
        //   url:"../my/info/info"
        // })
        this._jumpUrl();
      })
    })
    
  }
})
